#include "qmyslider.h"

#include "../../include/abstractproperty.h"
#include "../../lib/shared/qdatamanager.h"
#include "../../include/qcommonstruct.h"

#include <QVariant>
#include <QMouseEvent>

QSliderHost::QSliderHost(QWidget *wid):
    QObjectHost(wid)
{

}

int QSliderHost::value()
{
    return ((QAbstractSlider*)m_widget)->value();
}


QMySlider::QMySlider(QWidget *parent):
    QSlider(parent),
    m_host(new QSliderHost(this))
{
    setProperty("className","QSlider");
    setProperty("typeName","Slider");
    setProperty("typeIcon",":/images/widgets/vslider.png");
    setValue(0);
    resize(30,100);


    m_host->addProperty("Attribute",QStringList()<<"value"<<"font"<<"focusPolicy"<<"orientation"<<"maximum"<<"minimum");
    m_host->addProperty("Signals",QStringList()<<"valueChanged");
    m_host->addUserProperty(QStringList()<<"value");

    connect(this,SIGNAL(valueChanged(int)),this,SLOT(valueChanged_signal(int)));
}

void QMySlider::valueChanged_signal(int value)
{
    QString code=property("valueChanged").toString();
    if(code!="")
    {
        QString para=QString("type=valueChanged,num=%1,_button=%2").arg(1).arg(value);
        m_host->exec(code,para);
    }

    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="value")
            {
                manager->setUserData(info.m_valueName,value);

            }
        }
    }
}

void QMySlider::updateProperty(const QString & name)
{
    PropertyList list=property("property_list").value<PropertyList>();
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
    else if(name=="maximum")
    {

        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="minimum")
            {
                p->setAttribute("max",this->maximum());

            }
            else if(p->name()=="value")
            {
                QVariant v=this->property("value");
                if(v!=p->value())
                {
                    p->setValue(v);
                    p->setModified(true);
                }
                p->setAttribute("max",this->maximum());
            }
        }
    }
    else if(name=="minimum")
    {
        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="maximum")
            {
                p->setAttribute("min",this->minimum());
            }
            else if(p->name()=="value")
            {
                QVariant v=this->property("value");
                if(v!=p->value())
                {
                    p->setValue(v);
                    p->setModified(true);
                }
                p->setAttribute("min",this->minimum());
            }
        }
    }
}

void QMySlider::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="maximum")
        {
            p->setAttribute("min",this->minimum());
        }
        else if(name=="minimum")
        {
            p->setAttribute("max",this->maximum());
        }
        else if(name=="value")
        {
            p->setAttribute("min",this->minimum());
            p->setAttribute("max",this->maximum());
        }
        else if(name=="valueChanged")
        {
            p->setAttribute("param","valueChanged");
        }
    }
}


void QMySlider::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}


QMyDial::QMyDial(QWidget *parent):
    QDial(parent),
    m_host(new QSliderHost(this))
{
    setProperty("className","QDial");

    setProperty("typeName","dial");
    setProperty("typeIcon",":/images/widgets/dial.png");

    setValue(0);


    m_host->addProperty("Attribute",QStringList()<<"value"<<"font"<<"focusPolicy"<<"maximum"<<"minimum");
    m_host->addProperty("Signals",QStringList()<<"valueChanged");
    m_host->addUserProperty(QStringList()<<"value");


    connect(this,SIGNAL(valueChanged(int)),this,SLOT(valueChanged_signal(int)));
}

void QMyDial::valueChanged_signal(int value)
{
    QString code=property("valueChanged").toString();
    if(code!="")
    {
        QString para=QString("type=valueChanged,num=%1,_button=%2").arg(1).arg(value);
        m_host->exec(code,para);
    }
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="value")
            {
                manager->setUserData(info.m_valueName,value);

            }
        }
    }
}

void QMyDial::updateProperty(const QString & name)
{
    PropertyList list=property("property_list").value<PropertyList>();
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
    else if(name=="maximum")
    {

        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="minimum")
            {
                p->setAttribute("max",this->maximum());
            }
            else if(p->name()=="value")
            {
                QVariant v=this->property("value");
                if(v!=p->value())
                {
                    p->setValue(v);
                    p->setModified(true);
                }
                p->setAttribute("max",this->maximum());
            }
        }
    }
    else if(name=="minimum")
    {
        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="maximum")
            {
                p->setAttribute("min",this->minimum());
            }
            else if(p->name()=="value")
            {
                QVariant v=this->property("value");
                if(v!=p->value())
                {
                    p->setValue(v);
                    p->setModified(true);
                }
                p->setAttribute("min",this->minimum());
            }
        }
    }
}

void QMyDial::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="maximum")
        {
            p->setAttribute("min",this->minimum());
        }
        else if(name=="minimum")
        {
            p->setAttribute("max",this->maximum());
        }
        else if(name=="value")
        {
            p->setAttribute("min",this->minimum());
            p->setAttribute("max",this->maximum());
        }
        else if(name=="valueChanged")
        {
            p->setAttribute("param","valueChanged");
        }
    }
}

void QMyDial::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}
